package com.linkoog.devtools.http.document.doc_upload.showdoc;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationAction;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.linkoog.devtools.http.raw_index.HttpServiceMethod;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class ShowdocDocUploadAction extends AnAction {

    private static NotificationGroup notificationGroup;
    static {
        notificationGroup = new NotificationGroup("DevTools.HttpTest.Showdoc",  NotificationDisplayType.BALLOON, true);
    }

    private final HttpServiceMethod httpServiceMethod;

    public ShowdocDocUploadAction(@NotNull HttpServiceMethod httpServiceMethod, @NotNull String text, String description, Icon icon) {
        super(text, description, icon);
        this.httpServiceMethod = httpServiceMethod;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = e.getData(CommonDataKeys.PROJECT);
        if (project == null){
            return;
        }

        /*ShowdocConfig showdocConfig = TestDevToolsPersistent.getInstance(project).getShowdocConfig();
        String host = showdocConfig.getHost();
        if (StringUtils.isBlank(host)){
            showErrorAndSetting(project,"上传showdoc平台失败:<br/>" + "host未设置！", true);
            return;
        }

        String user = showdocConfig.getUser();
        String passwd = showdocConfig.getPasswd();
        if (StringUtils.isBlank(user) || StringUtils.isBlank(passwd)){
            showErrorAndSetting(project,"上传showdoc平台失败:<br/>" + "用户或密码未设置！", true);
            return;
        }

        ItemListVo showdocProject = showdocConfig.getProject();
        if (showdocProject == null || showdocProject.getItem_id() == null){
            showErrorAndSetting(project,"上传showdoc平台失败:<br/>" + "项目未设置！", true);
            return;
        }

        ShowdocHttp showdocHttp = new ShowdocHttp(host);
        String token = null;
        try {
            token = showdocHttp.getLoginToken(user,passwd);
            Assert.hasText(token, "token不能为空！");
        }catch (Throwable throwable){
            showErrorAndSetting(project,"上传showdoc平台失败:<br/>" + "登录失败，请检查配置！", true);
            return;
        }

        ItemInfoVo itemInfoVo = null;
        try {
            ShowdocResponse<ItemInfoVo> itemInfoVoShowdocResponse = showdocHttp.itemInfo(token, showdocProject.getItem_id());
            Assert.isTrue(itemInfoVoShowdocResponse.getError_code() != null && itemInfoVoShowdocResponse.getError_code().equals(0), itemInfoVoShowdocResponse.getError_message());
            itemInfoVo = itemInfoVoShowdocResponse.getData();
            Assert.isTrue(itemInfoVo != null, "错误的项目id");
        }catch (Throwable throwable){
            showErrorAndSetting(project,"上传showdoc平台失败:<br/>" + "获取项目信息失败，请检查配置！<br/>" + throwable.getMessage(), true);
            return;
        }

        ShowdocUploadDialog showdocUploadDialog = new ShowdocUploadDialog(httpServiceMethod, project, showdocHttp, token, itemInfoVo);
        showdocUploadDialog.show();*/
    }

    private void showErrorAndSetting(Project project, String errorMsg, boolean showSetting) {
        Notification error = notificationGroup.createNotification(errorMsg, NotificationType.INFORMATION);
        if (showSetting){
            error.addAction(new NotificationAction("Showdoc Settings") {
                @Override
                public void actionPerformed (@NotNull AnActionEvent anActionEvent, @NotNull Notification notification){
                    //DataContext dataContext = anActionEvent.getDataContext();
                    //Project project = PlatformDataKeys.PROJECT.getData(dataContext);
                    /*HttpTestSettingDialog dialog = new HttpTestSettingDialog(project);
                    dialog.showShowdocTab();
                    dialog.show();*/
                }
            });
        }
        Notifications.Bus.notifyAndHide(error, project);
    }


}
